<?php

class CommentService extends AbstractService
{

    protected function _init()
    {
	$this->setMapper(new CommentMapper());
    }

    public function getByReview(ReviewModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	$cacheSlot = new CacheSlotsReviewFeatures($model, 'comments');
	if (($comments = $cacheSlot->load()) === false) {
	    $comments = $this->makeCommentTree($this->getMapper()->getByReviewId($model->getId()));
	    if (!$comments) {
		$comments = array();
	    }
	    $cacheSlot->setData($comments)->save();
	}
	$model->setComments($comments);
    }

    public function getByCompany(CompanyModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	$cacheSlot = new CacheSlotsCompanyFeatures($model, 'comments');
	if (($comments = $cacheSlot->load()) === false) {
	    $comments = $this->makeCommentTree($this->getMapper()->getByCompanyId($model->getId()));
	    $cacheSlot->setData($comments)->save();
	}
	if ($comments) {
	    $model->setComments($comments);
	}
    }

    public function getByUser(UserModel $model, $page = 1, $limit = 10)
    {
	if (!$model->getId()) {
	    return false;
	}
//	$cacheSlot = new CacheSlotsUserFeatures($model, 'comments', array('page' => $page, 'limit' => $limit));
//	if (($comments = $cacheSlot->load()) === false) {
//	    $comments = $this->getMapper()->getByUserId($model->getId(), $page, $limit);
//	    foreach ($comments as $comment) {
//		$targetModel = FactoryService::get($comment->getDbData('target_type'))->get($comment->getDbData('target_id'));
//		$comment->setTarget($targetModel);
//	    }
//	    $cacheSlot->setData($comments)->save();
//	}
	$comments = $this->getMapper()->getByUserId($model->getId(), $page, $limit);
	foreach ($comments as $comment) {
	    $targetModel = FactoryService::get($comment->getDbData('target_type'))->get($comment->getDbData('target_id'));
	    $comment->setTarget($targetModel);
	}
	if ($comments) {
	    $model->setComments($comments);
	}
    }

    public function getCountByUser(UserModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	return $this->getCount(array('where' => 'user_id = :user_id', 'params' => array(':user_id' => $model->getId())));
    }

    public function getWishesComments(WishesModel $model)
    {
	$cacheSlot = new CacheSlotsWishesFeatures($model, 'comments');
	if (($comments = $cacheSlot->load()) === false) {
	    $comments = $this->makeCommentTree($this->getMapper()->getWishesComments());
	    $cacheSlot->setData($comments)->save();
	}
	if ($comments) {
	    $model->setComments($comments);
	}
    }

    public function makeCommentTree($comments, $parent = 0)
    {
	if (!$comments) {
	    return array();
	}
	$data = array();
	$allComments = $comments;
	foreach ($comments as $comment) {
	    if ($comment->parent_id == $parent) {
		$comment->setChildren($this->makeCommentTree($allComments, $comment->getId()));
		$data[$comment->getId()] = $comment;
	    }
	}
	return $data;
    }

    public function onAfterSave(AbstractModel $model = null)
    {
	if ($model->getId()) {
	    $cacheClass = 'CacheSlots' . ucfirst($model->target_type) . 'Features';
	    $targetModel = FactoryService::get($model->target_type)->get($model->target_id);
	    if (class_exists($cacheClass)) {
		if ($targetModel) {		    
		    $cacheSlot = new $cacheClass($targetModel, 'comments');
		    $cacheSlot->delete();		    
		}
	    }
	    if ($targetModel) {
		FactoryService::get('search')->toIndex(array(
		    'id' => 'comment' . $model->getId(),
		    'type' => 'comment',
		    'url' => Yii::app()->createUrl($model->getDbData('target_type') . '/view', array('id' => $targetModel->getId(), 'url' => $targetModel->getDbData('url'))),
		    'text' => $model->getDbData('content'),
		    'title' => $targetModel->getDbData('title')
		));
	    }
	}
	return true;
    }

}